import {
  formatHour
} from '../../utils/util.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    courseList: [],
    keyWords: [],
    showKeyWords: true,
    searchText: '',
    statusHeight: 0, //状态栏高度
    navHeight: 0 //标题栏高度

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      statusHeight: app.globalData.statusHeight,
      navHeight: app.globalData.navHeight
    })
    this.getSearchHistory();

  },
  searchKeyWords(e) {
    const searchText = e.currentTarget.dataset.item;
    this.setData({
      searchText,
      showKeyWords: false
    })
    this.search();
  },
  // 搜索历史
  getSearchHistory() {
    app.fetchRequest({
      url: `/course/keyword`,
      data: {},
      success: (res) => {
        this.setData({
          keyWords: res
        })
      }
    })
  },
  search() {
    app.fetchRequest({
      url: '/course/query',
      data: {
        courseName: this.data.searchText,
      },
      success: (res) => {
        res.list.forEach(item => {
          item.totalDuration = formatHour(item.totalDuration);
        })
        this.setData({
          courseList: res.list
        })
      }
    })


  },
  doSearch(e) {
    if (e && e.detail.value) {
      this.setData({
        searchText: e.detail.value
      })
      this.search();
    }
  },
  toCourse(e) {
    const courseId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `../course/course?courseId=${courseId}`,
    })
  },
  navigateBack() {
    wx.navigateBack()
  }
})